//
//  RAAppVersionUtil.m
//  RAFSUManagement
//
//  Created by Piccolo on 02/04/2018.
//  Copyright © 2018 Rayonics. All rights reserved.
//

#import "RAAppVersionUtil.h"
#import <PgyUpdate/PgyUpdateManager.h>

@implementation RAAppVersionUtil

+ (NSRAAppVersionUpdateType)appVersionUpdateType:(NSString *)pgyAppVersion {
    
    NSArray<NSString *>*pgyAppVersionStrArray = [pgyAppVersion componentsSeparatedByString:@"."];
    NSInteger pgyLargestVerionNumber = [pgyAppVersionStrArray[0] integerValue];
    NSInteger pgyNecessaryVersionNumner = [pgyAppVersionStrArray[1] integerValue];
    NSInteger pgyMinorVersionNumber = [pgyAppVersionStrArray[2] integerValue];

    NSArray<NSString *>*currentVersionStrArray = [APP_VERSION componentsSeparatedByString:@"."];
    NSInteger currentLocalLargestVerionNumber = [currentVersionStrArray[0] integerValue];
    NSInteger currentNecessaryVersionNumner = [currentVersionStrArray[1] integerValue];
    NSInteger currentMinorVersionNumber = [currentVersionStrArray[2] integerValue];
    
    if (pgyLargestVerionNumber > currentLocalLargestVerionNumber || pgyNecessaryVersionNumner > currentNecessaryVersionNumner) {
        return NSRAAppVersionUpdateTypeNecessary;
    } else if (pgyMinorVersionNumber > currentMinorVersionNumber) {
        if (pgyMinorVersionNumber - currentMinorVersionNumber > 1) {
            return NSRAAppVersionUpdateTypeNecessary;
        } else {
            return NSRAAppVersionUpdateTypeMinor;
        }
    } else if (pgyLargestVerionNumber == currentLocalLargestVerionNumber &&
               pgyNecessaryVersionNumner == currentNecessaryVersionNumner &&
               pgyMinorVersionNumber == currentMinorVersionNumber) {
        return NSRAAppVersionUpdateTypeSame;
    } else {
        return NSRAAppVersionUpdateTypeNecessary;
    }
}

+ (void)updateApp {

    [[PgyUpdateManager sharedPgyManager] checkUpdate];
}

@end
